<template>
  <div class="dashboard">
    <div class="dashboard-header">
      <h1>欢迎使用DevOps管理平台</h1>
      <el-button @click="loadDashboardData" :loading="loading" type="primary">
        <el-icon><Refresh /></el-icon>
        刷新数据
      </el-button>
    </div>
    
    <el-row :gutter="20" class="stats-row">
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon size="40" color="#409EFF"><Monitor /></el-icon>
            </div>
            <div class="stat-info">
              <h3>{{ stats.totalHosts }}</h3>
              <p>主机总数</p>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon size="40" color="#67C23A"><User /></el-icon>
            </div>
            <div class="stat-info">
              <h3>{{ stats.totalUsers }}</h3>
              <p>用户总数</p>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon size="40" color="#E6A23C"><DataBoard /></el-icon>
            </div>
            <div class="stat-info">
              <h3>{{ stats.totalDatabases }}</h3>
              <p>数据库实例</p>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon size="40" color="#F56C6C"><Document /></el-icon>
            </div>
            <div class="stat-info">
              <h3>{{ stats.pendingSQLs }}</h3>
              <p>待审核SQL</p>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" class="content-row">
      <el-col :span="12">
        <el-card>
          <template #header>
            <span>最近登录记录</span>
          </template>
          <el-table :data="recentLogins" style="width: 100%" v-loading="loading">
            <el-table-column prop="username" label="用户名" width="100" />
            <el-table-column prop="host" label="主机" width="120" />
            <el-table-column prop="ip_address" label="IP地址" width="130" />
            <el-table-column prop="login_time" label="登录时间" />
            <template #empty>
              <div class="empty-data">
                <el-icon><Monitor /></el-icon>
                <p>暂无登录记录</p>
              </div>
            </template>
          </el-table>
        </el-card>
      </el-col>
      
      <el-col :span="12">
        <el-card>
          <template #header>
            <span>主机状态分布</span>
          </template>
          <div class="host-status">
            <div class="status-item">
              <span class="status-dot online"></span>
              <span>在线: {{ hostStatus.online }}</span>
            </div>
            <div class="status-item">
              <span class="status-dot offline"></span>
              <span>离线: {{ hostStatus.offline }}</span>
            </div>
            <div class="status-item">
              <span class="status-dot maintenance"></span>
              <span>维护中: {{ hostStatus.maintenance }}</span>
            </div>
            <div class="status-item">
              <span class="status-dot error"></span>
              <span>异常: {{ hostStatus.error }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Monitor, User, DataBoard, Document, Refresh } from '@element-plus/icons-vue'
import { cmdbAPI } from '../api/cmdb'

const stats = ref({
  totalHosts: 0,
  totalUsers: 0,
  totalDatabases: 0,
  pendingSQLs: 0
})

const recentLogins = ref([])

const hostStatus = ref({
  online: 0,
  offline: 0,
  maintenance: 0,
  error: 0
})

const loading = ref(false)

const loadDashboardData = async () => {
  try {
    loading.value = true
    console.log('开始加载仪表板数据...')

    const response = await cmdbAPI.getDashboardStats()

    if (response.stats) {
      stats.value = response.stats
    }

    if (response.hostStatus) {
      hostStatus.value = response.hostStatus
    }

    if (response.recentLogins) {
      recentLogins.value = response.recentLogins
    }

    console.log('仪表板数据加载完成:', response)

  } catch (error) {
    console.error('加载仪表盘数据失败:', error)
    ElMessage.error('加载仪表盘数据失败，请检查网络连接')

    // 加载失败时使用默认数据
    stats.value = {
      totalHosts: 0,
      totalUsers: 0,
      totalDatabases: 0,
      pendingSQLs: 0
    }

    hostStatus.value = {
      online: 0,
      offline: 0,
      maintenance: 0,
      error: 0
    }

    recentLogins.value = []
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadDashboardData()
})
</script>

<style scoped>
.dashboard {
  padding: 20px;
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.dashboard-header h1 {
  margin: 0;
  color: #333;
}

.empty-data {
  text-align: center;
  padding: 20px;
  color: #999;
}

.empty-data .el-icon {
  font-size: 48px;
  margin-bottom: 10px;
}

.stats-row {
  margin-bottom: 30px;
}

.stat-card {
  height: 120px;
}

.stat-content {
  display: flex;
  align-items: center;
  height: 100%;
}

.stat-icon {
  margin-right: 20px;
}

.stat-info h3 {
  margin: 0 0 5px 0;
  font-size: 28px;
  color: #333;
}

.stat-info p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.content-row {
  margin-top: 20px;
}

.host-status {
  padding: 20px 0;
}

.status-item {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.status-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  margin-right: 10px;
}

.status-dot.online {
  background-color: #67C23A;
}

.status-dot.offline {
  background-color: #909399;
}

.status-dot.maintenance {
  background-color: #E6A23C;
}

.status-dot.error {
  background-color: #F56C6C;
}
</style>
